package jmine.tec.utils.io;

import java.io.FilterOutputStream;
import java.io.IOException;
import java.io.OutputStream;

/**
 * @author takeshi
 */
public class CountingOutputStream extends FilterOutputStream {

    private long transfered;

    /**
     * C'tor
     * 
     * @param out the outputstream delegate
     */
    public CountingOutputStream(OutputStream out) {
        super(out);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void write(byte[] b) throws IOException {
        this.out.write(b);
        synchronized (this) {
            this.transfered += b.length;
        }
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void write(byte[] b, int off, int len) throws IOException {
        this.out.write(b, off, len);
        synchronized (this) {
            this.transfered += len;
        }
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void write(int b) throws IOException {
        this.out.write(b);
        synchronized (this) {
            this.transfered++;
        }
    }

    /**
     * @return the transfered
     */
    public synchronized long getTransfered() {
        return this.transfered;
    }

}
